<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/22
 * Time: 0:45
 */
namespace app\admin\model\getdatas;
use app\admin\builder\Builder;
use think\Exception;
use think\facade\Db;

class Mydata
{
    public $data = [];
    public $model =[];
    protected $model_id = '';
    protected $cid = '';
    protected $field = [];
    protected $cidInfo = [];

    /**设置联表的id
     * @param string设置当前的模型id $id
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setModelId($id=''){
        if($id){
            $this->cid = '';
            $this->model_id = $id;
            $this->model = Db::name(getTable('models'))
                ->where('id',$id)
                ->find();
            $this->model['toops'] = strToArray($this->model['toops'],',');
            $this->model['right'] = strToArray($this->model['right'],',');
            $this->setField();
        }

        return $this;
    }

    /**设置用户栏目id
     * @param string $cid
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setCid($cid=''){
        if($cid){
            $this->model_id = '';
            $this->cid = $cid;
            $this->cidInfo = Db::name(getTable('column'))
                ->where('id',$this->cid)
                ->find();
            $this->model = Db::name(getTable('models'))
                ->where('id',$this->cidInfo['model_id'])
                ->find();
            $this->model['toops'] = strToArray($this->model['toops'],',');
            $this->model['right'] = strToArray($this->model['right'],',');
            $this->setField();
        }

        return $this;
    }

    /**设置表字段
     * @return $this
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function setField(){
//        echo $this->cid;exit;
        if($this->model_id){
            $this->field = Db::name(getTable('fields'))
                ->where('model_id',$this->model_id)
                ->select()->toArray();
//            echo '<pre>';
//            print_r($this->field);
//            exit;
        }elseif ($this->cid){
            $this->field = Db::name(getTable('ziduancid'))
                ->where('cid',$this->cid)
                ->select()->toArray();
//            echo '<pre>';
//            print_r($this->field);
//            exit;
        }else{
            new Exception('没有设置表');
        }
        return $this;
    }

    /**获取模型
     * @return array
     */
    public function getModel(){
        return $this->model;
    }

    /**获取字段
     * @return array
     */
    public function getField(){
        return $this->field;
    }

    /**获取栏目详情
     * @return array
     */
    public function getCid(){
        return $this->cidInfo;
    }
    public function getForm($action='',$id=null,$edit=true,$buxianshi=[],$pid=null){
        $builder = Builder::form()
            ->setAction($action);
        $field = $this->getField();
        $table = $this->getModel('table');
        $data = null;
        if($id){
            $data = Db::name($table)
                ->where('id',$id)
                ->find();
            $builder = $builder->setProject('none','id','',$data['id']);
        }
//        echo '<pre>';
//        print_r($field);
//        exit;
        foreach ($field as $value){
            if(in_array($value['field'],$buxianshi)){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
                continue;
            }
            if(!$value['edit']){
                if($data){
                    $builder = $builder->setProject('none',$value['field'],'',$data[$value['field']]);
                }else{
                    $builder = $builder->setProject('none',$value['field'],'',$value['defaults']);
                }
            }else{
                if($value['form_type']=='单行文本'){
                    if($data){
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('input',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }

                if($value['form_type']=='多行文本'){
                    if($data){
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$data[$value['field']],'',$value['descs']);
                    }else{
                        $builder = $builder->setProject('textarea',$value['field'],$value['field_name'],$value['defaults'],'',$value['descs']);
                    }
                }

//单文件
                if($value['form_type']=='单文件'){
                    if($data){
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('file',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //多文件
                if($value['form_type']=='多文件'){
                    if($data){
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('files',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='单图片'){
                    if($data){
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('img',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='视频'){
                    if($data){
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('video',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                if($value['form_type']=='多图片'){
                    if($data){
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('imgs',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }

                //单选框
                if($value['form_type']=='单选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //多选框
                if($value['form_type']=='多选框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //下拉框
                if($value['form_type']=='下拉框'){
                    $array = explode(',',$value['option']);
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs']);
                    }
                }
                //联动单选框
                if($value['form_type']=='联动单选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('radio',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //联动多选框
                if($value['form_type']=='联动多选框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
                    $array = [];
                    if($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }
                    if($data){
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('checkbox',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }


                if($value['form_type']=='联动下拉框'){
                    $mytable = Db::name(getTable('models'))->where('id',$value['table'])->find();
//                    echo '<pre>';
//                    print_r($mytable);
//                    exit;
                    $array = [];
                    if($mytable['id']??null&&$mytable['id']==14){
                        $array = Db::name($mytable['table'])->where('status',1)->field('id,name')->select()->toArray();
                    }elseif($mytable){
                        $array = Db::name($mytable['table'])->where('status',1)->where('delete_time',0)->field('id,name')->select()->toArray();
                    }
                    if($this->model_id==2&&$mytable['id']==10){
                        $array = Db::name($mytable['table'])->where('type','<>','1')->where('status',1)->where('delete_time',0)->field('id,name')->select()->toArray();
                    }
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    if($data){
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        if($value['field']=='pid'){
                            if($pid){
                                $builder = $builder->setProject('select','pid',$value['field_name'],$pid,$array,$value['descs'],true);
                                continue;
                            }
                        }
                        $builder = $builder->setProject('select',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }

//联动下拉组
                if($value['form_type']=='联动下拉组'){
                    $tablesid =  explode(',',$value['table']);
                    $table1 = Db::name(getTable('models'))->where('id',$tablesid[0])->find();
                    if($tablesid[1]??null){
                        $table2 = Db::name(getTable('models'))->where('id',$tablesid[1])->find();
                    }else{
                        echo '联动下拉组最少联两表';
                    }
                    $array = Db::name($table1['table'])->where('status',1)->field('id,name')->select()->toArray();

                    foreach ($array as $key=>$va){
                        $va['child'] = Db::name($table2['table'])->where('status',1)->where($value['hierarchy'],$va['id'])->field('id,name')->select()->toArray();
                        $array[$key] = $va;
                    }
                    if($data){
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$data[$value['field']],$array,$value['descs'],true);
                    }else{
                        $builder = $builder->setProject('optgroup',$value['field'],$value['field_name'],$value['defaults'],$array,$value['descs'],true);
                    }
                }
                //编辑框
                if($value['form_type']=='编辑框'){
                    if($data){
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editor',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //编辑框(layui)
                if($value['form_type']=='编辑框(layui)'){
                    if($data){
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$data[$value['field']],[],$value['descs']);
                    }else{
                        $builder = $builder->setProject('editors',$value['field'],$value['field_name'],$value['defaults'],[],$value['descs']);
                    }
                }
                //开关
                if($value['form_type']=='开关'){
                    if($data){
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('switch',$value['field'],$value['field_name'],$value['defaults'],['开启','关闭'],$value['descs']);
                    }
                }

                //时间
                if($value['form_type']=='时间'){
                    if($data){
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],$data[$value['field']],['开启','关闭'],$value['descs']);
                    }else{
                        $builder = $builder->setProject('time',$value['field'],$value['field_name'],time(),['开启','关闭'],$value['descs']);
                    }
                }
                if($value['form_type']=='json数组'){
                    $array = explode(':',$value['option']);
//                    echo '<pre>';
//                    print_r($array);
//                    exit;
                    if($data){
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($data[$value['field']],true),$array,$value['descs']);
                    }else{
                        $builder = $builder->setProject('json',$value['field'],$value['field_name'],json_decode($value['defaults'],true),$array,$value['descs']);
                    }
                }

            }
        }
//        echo 'haha';exit;
        if($edit){
            $builder = $builder->setButton('submit','提交');
        }
        $builder = $builder->setButton('back','返回');
        return $builder->fetch();
    }
}